﻿using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace laohan.Common.Common
{
    public partial class API
    {
        /// <summary>
        /// 
        /// </summary>
        public static FastZip fz = new FastZip();

        /// <summary>
        /// 
        /// </summary>
        /// <param name="zipFilePath"></param>
        /// <param name="password"></param>
        public static void Compress(string zipFilePath, string password)
        {
            try
            {
                fz.Password = password;

                var path = Path.GetDirectoryName(zipFilePath);

                fz.ExtractZip(zipFilePath, path, "");
            }
            catch (Exception ex)
            {
                throw new Exception("解压文件发生错误，错误原因：" + ex.Message);
            }
        }

        /// <summary>
        /// 解压RAR和ZIP文件(需存在Winrar.exe(只要自己电脑上可以解压或压缩文件就存在Winrar.exe))
        /// </summary>
        /// <param name="rarPathName">待解压文件存放绝对路径（包括文件名称）</param>
        /// <param name="IsCover">所解压的文件是否会覆盖已存在的文件(如果不覆盖,所解压出的文件和已存在的相同名称文件不会共同存在,只保留原已存在文件)</param>
        /// <param name="PassWord">解压密码(如果不需要密码则为空)</param>
        /// <returns>true(解压成功);false(解压失败)</returns>
        public static bool UnRarOrZip(string rarPathName, bool IsCover, string PassWord)
        {
            var UnPath = Path.GetDirectoryName(rarPathName);

            if (!Directory.Exists(UnPath))
            {
                Directory.CreateDirectory(UnPath);
            }

            Process Process1 = new Process();
            Process1.StartInfo.FileName = "Winrar.exe";
            Process1.StartInfo.CreateNoWindow = true;
            Process1.StartInfo.UseShellExecute = false;
            string cmd = "";

            if (!string.IsNullOrEmpty(PassWord) && IsCover)
            {
                //解压加密文件且覆盖已存在文件( -p密码 )
                cmd = string.Format(" x -p{0} -o+ {1} {2} -y", PassWord, rarPathName, UnPath);
            }
            else if (!string.IsNullOrEmpty(PassWord) && !IsCover)
            {
                //解压加密文件且不覆盖已存在文件( -p密码 )
                cmd = string.Format(" x -p{0} -o- {1} {2} -y", PassWord, rarPathName, UnPath);
            }
            else if (IsCover)
            {
                //覆盖命令( x -o+ 代表覆盖已存在的文件)
                cmd = string.Format(" x -o+ {0} {1} -y", rarPathName, UnPath);
            }
            else
            {
                //不覆盖命令( x -o- 代表不覆盖已存在的文件)
                cmd = string.Format(" x -o- {0} {1} -y", rarPathName, UnPath);
            }

            Process1.StartInfo.Arguments = cmd;//命令
            Process1.Start();
            Process1.WaitForExit();//无限期等待进程 winrar.exe 退出
                                   //Process1.ExitCode==0指正常执行，Process1.ExitCode==1则指不正常执行
            if (Process1.ExitCode == 0)
            {
                Process1.Close();
                return true;
            }
            else
            {
                Process1.Close();
                return false;
            }

        }
    }
}
